package com.tom;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class AddPostServlet
 */
public class AddPostServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddPostServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String userid=null;
		String nickname=null;
		String title=null;
		String content=null;
		
		
		if (request.getParameter("userid")!=null){
            userid = request.getParameter("userid");
            nickname = request.getParameter("nickname");
            title = request.getParameter("title");
            content = request.getParameter("content");
            HttpSession session = request.getSession();
            Member m = (Member) session.getAttribute("m");
            
            		   		
		Post p = new Post(userid, nickname,title, content);
        session.setAttribute("p", p);
		
        response.sendRedirect("add_post.jsp"); 
        boolean saved = p.save();
        if (saved){
                //成功
                
        }else{
                //留言新增失敗
        }
		} 
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request ,  response);
	}

}
